Ext.define('Seq.echarts.Base', {
    extend: 'Ext.Component',
    xtype: 'echartsbase',

    config: {
        theme: 'macarons',
        value: null,
        url: null,
        forceClear: false // 每次绘制前，强制清空option
    },
    minWidth: 240,
    minHeight: 160,
    defaultBindProperty: 'value',

    calcxAxis(xAxis = []) { // 通过函数计算X轴
        for (let axis of xAxis) {
            if (axis.func) {
                axis.data = eval(axis.func);
            }
        }
    },

    mergeArray(to, from) {
        if (from && to) {
            let len = from.length;
            for (let i = 0; i < len; i++) {
                Ext.applyIf(to[i], from[i]);
            }
        }
    },

    dataScale(series = []) {
        for (let s of series) {
            if (s.data) {
                let scale = s.scale ? s.scale : 1, format = s.format ? s.format : '0.##';
                for (let i in s.data) {
                    let d = s.data[i];
                    if (Ext.isEmpty(d)) {
                        continue;
                    }
                    if (Ext.isNumeric(d)) {
                        s.data[i] = Ext.util.Format.number(d / scale, format);
                    } else if (Ext.isNumeric(d.value)) {
                        d.value = Ext.util.Format.number(d.value / scale, format);
                    }
                }
            }
        }
    },

    mergeValue(me, option, value) {
        // 使用前台设置
        option.title && value.title && Ext.apply(value.title, option.title);
        option.legend && value.legend && Ext.apply(value.legend, option.legend);
        option.bmap && value.bmap && Ext.apply(value.bmap, option.bmap);
        // 使用后台设置，前台显示地图的名称(例如:sichuan)，需要后台决定
        option.visualMap && value.visualMap && Ext.applyIf(value.visualMap, option.visualMap);
        option.geo && value.geo && Ext.applyIf(value.geo, option.geo);
        // 合并数组参数
        me.mergeArray(value.xAxis, option.xAxis);
        me.mergeArray(value.yAxis, option.yAxis);
        me.mergeArray(value.xAxis3D, option.xAxis3D);
        me.mergeArray(value.yAxis3D, option.yAxis3D);
        me.mergeArray(value.zAxis3D, option.zAxis3D);
        // 合并Series配置
        if (option.series && value.series) {
            let series = option.series, len = value.series.length;
            for (let i = 0; i < len; i++) {
                let s = series[i];
                if (s && s.name) {
                    value.series[i].name = s.name;
                }
                Ext.applyIf(value.series[i], s || series[0]); // 页面series可以共用一个配置
            }
        }
    },

    echartsPaint(me, value) {
        // 合并默认配置（option）和自定义配置(customer)以及后台的value
        let option = Ext.apply(Ext.clone(me.option), me.custom);
        me.mergeValue(me, option, value);
        // 处理后台数据
        me.dataScale(value.series);
        me.calcxAxis(value.xAxis);
        me.echartsRefresh(me, option, value);
    },

    echartsRefresh(me, option, value) {
        let echart = me.echart;
        if (echart && me.getForceClear()) {
            me.echart = null;
            echart.dispose();
            echart = null;
        }
        if (null == echart) {
            echart = echarts.init(Ext.fly(me.getId()).dom, me.getConfig('theme'));
            me.echart = echart;
            echart.setOption(option);
            // 鼠标点击事件
            echart.on('click', function (params) {
                me.fireEvent('click', me, params);
            });
            // 初始化完毕事件
            me.fireEvent('afterinit', me);
        }
        value && echart.setOption(value);
    },

    listeners: {
        afterrender(cmp) {
            cmp.on('resize', (me) => {
                me.echart && me.echart.resize({width: me.getWidth(), height: me.getHeight()});
            });
        }
    },

    updateValue(value) {
        if (value) {
            let me = this;
            me.echartsPaint(me, value);
        }
    },

    updateUrl(value) {
        if (value) {
            let me = this, options = {
                fnSuccess(options, result) {
                    me.echartsPaint(me, result.data ? result.data : result);
                }
            };
            if (Ext.isString(value)) {
                options.url = value;
            } else {
                Ext.apply(options, value);
            }
            // 加载数据
            Seq.Ajax.request(options);
        }
    },

    clearValue() { // 防止Card布局，翻页时重绘
        this.value = null;
    },

    setData(data) {
        this.updateValue(data);
    }

});